﻿
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SqlFormat;

namespace SqlFormatTest {
    [TestClass]
    public class SqlFormatterTest {
        [TestMethod]
        public void TestModifyWhiteSpaceToSpaces() {
            MockSqlFormatter formatter = new MockSqlFormatter(new SqlFormatterOptions());
            string results = formatter.Format("a\n\n \t  \t\t\nb c");
            Assert.AreEqual("a b c", results);
        }

        [TestMethod]
        public void TestFormatStrings() {
            MockSqlFormatter formatter = new MockSqlFormatter(new SqlFormatterOptions());
            formatter._BeforeString = "-startstring-";
            formatter._AfterString = "-endstring-";
            string results = formatter.Format("aa\"string\"bb");
            Assert.AreEqual("aa-startstring-\"string\"-endstring-bb", results);
        }

        [TestMethod]
        public void TestFormatStringsSingleQuotes() {
            MockSqlFormatter formatter = new MockSqlFormatter(new SqlFormatterOptions());
            formatter._BeforeString = "-startstring-";
            formatter._AfterString = "-endstring-";
            string results = formatter.Format("aa'string'bb");
            Assert.AreEqual("aa-startstring-'string'-endstring-bb", results);
        }


        [TestMethod]
        public void TestFormatKeywords() {
            MockSqlFormatter formatter = new MockSqlFormatter(new SqlFormatterOptions());
            formatter._BeforeKeyWord = "-startkeyword-";
            formatter._AfterKeyWord = "-endkeyword-";
            string results = formatter.Format("select aaSELECTbb select");
            Assert.AreEqual("-startkeyword-SELECT-endkeyword- aaSELECTbb -startkeyword-SELECT-endkeyword-", results);
        }

        [TestMethod]
        public void TestFormatKeywordsInString() {
            MockSqlFormatter formatter = new MockSqlFormatter(new SqlFormatterOptions());
            formatter._BeforeKeyWord = "-startkeyword-";
            formatter._AfterKeyWord = "-endkeyword-";
            string results = formatter.Format("aa'select'bb");
            Assert.AreEqual("aa'select'bb", results);
        }

        [TestMethod]
        public void TestFormatSql() {
            MockSqlFormatter formatter = new MockSqlFormatter(new SqlFormatterOptions());
            formatter._BeforeKeyWord = "-startkeyword-";
            formatter._AfterKeyWord = "-endkeyword-";
            formatter._BeforeString = "-startstring-";
            formatter._AfterString = "-endstring-";
            string results = formatter.Format("Select *   From Director_Film_Table  Where Director_Last_Name=\"Kubrick\";");
            Assert.AreEqual("-startkeyword-SELECT-endkeyword- * -startkeyword-FROM-endkeyword- Director_Film_Table -startkeyword-WHERE-endkeyword- Director_Last_Name=-startstring-\"Kubrick\"-endstring-;", results);
        }

        [TestMethod]
        public void TestFormatSqlMultilineSelect() {
            SqlFormatterOptions options = new SqlFormatterOptions();
            options.Multiline = true;
            MockSqlFormatter formatter = new MockSqlFormatter(options);
            string results = formatter.Format("Select *   From Director_Film_Table  Where Director_Last_Name=\"Kubrick\";");
            string expected = "SELECT\n\t*\nFROM\n\tDirector_Film_Table\nWHERE\n\tDirector_Last_Name=\"Kubrick\";";
            Assert.AreEqual(expected, results);
        }

        [TestMethod]
        public void TestFormatSqlMultilineDelete() {
            SqlFormatterOptions options = new SqlFormatterOptions();
            options.Multiline = true;
            MockSqlFormatter formatter = new MockSqlFormatter(options);
            string results = formatter.Format("Delete From Director_Film_Table Where Director_Last_Name=\"Kubrick\";");
            string expected = "DELETE FROM\n\tDirector_Film_Table\nWHERE\n\tDirector_Last_Name=\"Kubrick\";";
            Assert.AreEqual(expected, results);
        }

        [TestMethod]
        public void TestFormatSqlMultilineInsert() {
            SqlFormatterOptions options = new SqlFormatterOptions();
            options.Multiline = true;
            MockSqlFormatter formatter = new MockSqlFormatter(options);
            string results = formatter.Format("Insert Into Thespian_Film_Table (Thespian_Last_Name, Thespian_First_Name, Film_Title) Values (\"Travolta\", \"John\", \"Pulp Fiction\");");
            string expected = "INSERT INTO\n\tThespian_Film_Table (Thespian_Last_Name, Thespian_First_Name, Film_Title)\nVALUES\n\t(\"Travolta\", \"John\", \"Pulp Fiction\");";
            Assert.AreEqual(expected, results);
        }

        [TestMethod]
        public void TestFormatSqlMultilineUpdate() {
            SqlFormatterOptions options = new SqlFormatterOptions();
            options.Multiline = true;
            MockSqlFormatter formatter = new MockSqlFormatter(options);
            string results = formatter.Format("Update Thespian_Film_Table Set Thespian_Last_Name = \"Palin\" where Thespian_First_Name = \"Michael\" and Thespian_Last_Name = \"Balin\";");
            string expected = "UPDATE\n\tThespian_Film_Table\nSET\n\tThespian_Last_Name = \"Palin\"\nWHERE\n\tThespian_First_Name = \"Michael\"\n\tAND Thespian_Last_Name = \"Balin\";";
            Assert.AreEqual(expected, results);
        }
    }
}
